<script>
import { isObject } from "lodash";
import comMixin from "./comMixin.js";
export default {
  mixins: [comMixin],
  props: {
    ischecked: {
      type: String,
      default: "ischecked",
    },
    width: {
      type: String | Number,
      default: "8",
    },
    isSingle: {
      type: Boolean,
      default: false
    },
    onRowChange: {
      type: Function,
      default: () => null
    }
  },
  computed: {
    dicDataSync() {
      if (!this.dicData.length) {
        return [
          {
            [this.filed]: $AONE_LAN.all,
            [this.returnFiled]: 0,
            [this.ischecked]: false,
          },
        ];
      }
      return this.dicData.map((it) => {
        if (!isObject(it)) {
          return {
            [this.filed]: it,
            [this.returnFiled]: it,
            [this.ischecked]: false,
          };
        }
        return it;
      });
    },
  },
  watch: {
    formData: {
      immediate: true,
      deep: true,
      handler(val) {
        if (this.dicDataSync) {
          const value = this.formData[this.prop];
          // 重置时
          if (!value) {
            this.$refs.selectRow?.clres?.();
          }
          this.value = value;
        }
      },
    },
  },
  methods: {
    onChange(valueIdArr) {
      // 排除 全部=0
      this.formData[this.prop] = valueIdArr.filter(Boolean);
      if (this.onRowChange) {
        this.onRowChange(valueIdArr)
      }
    },
    renderLabel() {
      return this.label ? (
        <p class="label">
          {this.require ? <span class="red">*</span> : null}
          {this.label}：
        </p>
      ) : null;
    },
  },
  render() {
    return (
      <div class="aone-form-item">
        <div class="selectrow">
          { /*this.renderLabel()*/ }
          <w-select-row
            key={`sr${this.dicDataSync.length}`}
            ref="selectRow"
            borderColor="#bdbdbd"
            filed={this.filed}
            returnFiled={this.returnFiled}
            ischecked={this.ischecked}
            placeholder={this.placeholderSync}
            height={`${this.height}`}
            width={`${this.width}`}
            hwd={this.hwd}
            options={this.dicDataSync}
            onChange={this.onChange}
            isSingle={this.isSingle}
          ></w-select-row>
        </div>
      </div>
    );
  },
};
</script>
